<?php

class App_Model_DbTable_User extends Ext_Db_Table  
{  
    /** Table name */  
    protected $_name = 'user';
    
    private static $_userRow = array();
    
    public function getRowById($userId)
    {
        if(!isset($userId))
        {
            return "";
        }
        
        if(isset(self::$_userRow['id'][$userId]))
        {
            return self::$_userRow['id'][$userId];
        }
        
        $db = $this->getAdapter();
        $safe = $db->quoteInto(" id = ? ", $userId);
        $row = $this->fetchRow($safe);
        if (!$row) {
            return "";
        }
        $_userRow = $row->toArray();

        self::$_userRow['id'][$userId] = $_userRow;
        
        return $_userRow;
    }
        
    public function getAll()
    {
        $db = $this->getAdapter();
        $select = $db->select();
        $select
            ->from($this->_name, "*")
            ->order('id DESC')
            ;
        
        $list = $db->fetchAll($select);
        return $list;
    }
    
    public function addNew($inputArray)
    {
        $sql_data_array = array();
        if(array_key_exists('nickname', $inputArray)) $sql_data_array['nickname'] = $inputArray['nickname'];
        if(array_key_exists('roleId', $inputArray)) $sql_data_array['roleId'] = $inputArray['roleId'];
        if(!$sql_data_array){
            return "";
        }
        $sql_data_array['domain'] = '';
        
        try {
            $rows_affected = $this->insert($sql_data_array);
            $last_insert_id = $this->getAdapter()->lastInsertId();
        }catch (Exception $e){
            trigger_error($e->getMessage(), E_USER_WARNING);
        }
        return $last_insert_id;
    }
    
    public function addOld($inputArray, $userId)
    {
        $sql_data_array = array();
        if(array_key_exists('nickname', $inputArray)) $sql_data_array['nickname'] = $inputArray['nickname'];
        if(array_key_exists('roleId', $inputArray)) $sql_data_array['roleId'] = $inputArray['roleId'];
        if(array_key_exists('domain', $inputArray)) $sql_data_array['domain'] = $inputArray['domain'];
        if(!$sql_data_array)
        {
            return "";
        }
        
        try {
            $db = $this->getAdapter();
            $where = $db->quoteInto(" id = ? ", $userId);
            $rows_affected = $this->update($sql_data_array, $where);
        }catch (Exception $e){
            trigger_error($e->getMessage(), E_USER_WARNING);
        }
        
        return "";
    }
}  

